
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Go 语言函数 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../part12/" />
    
    
    <link rel="prev" href="../part10/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../part1/">
            
                <a href="../part1/">
            
                    
                    Go 语言简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../part2/">
            
                <a href="../part2/">
            
                    
                    Go 语言环境安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../part3/">
            
                <a href="../part3/">
            
                    
                    Go 语言结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../part4/">
            
                <a href="../part4/">
            
                    
                    Go 语言基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../part5/">
            
                <a href="../part5/">
            
                    
                    Go 语言数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../part6/">
            
                <a href="../part6/">
            
                    
                    Go 语言变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../part7/">
            
                <a href="../part7/">
            
                    
                    Go 语言常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../part8/">
            
                <a href="../part8/">
            
                    
                    Go 语言运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../part9/">
            
                <a href="../part9/">
            
                    
                    Go 语言条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../part10/">
            
                <a href="../part10/">
            
                    
                    Go 语言循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.12" data-path="./">
            
                <a href="./">
            
                    
                    Go 语言函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../part12/">
            
                <a href="../part12/">
            
                    
                    Go 语言数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../part13/">
            
                <a href="../part13/">
            
                    
                    Go 语言指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../part14/">
            
                <a href="../part14/">
            
                    
                    Go 语言结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../part15/">
            
                <a href="../part15/">
            
                    
                    Go 语言切片(Slice)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../part16/">
            
                <a href="../part16/">
            
                    
                    Go 语言范围(Range)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../part17/">
            
                <a href="../part17/">
            
                    
                    Go 语言Map(集合)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../part18/">
            
                <a href="../part18/">
            
                    
                    Go 语言递归函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../part19/">
            
                <a href="../part19/">
            
                    
                    Go 语言类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../part20/">
            
                <a href="../part20/">
            
                    
                    Go 语言接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../part21/">
            
                <a href="../part21/">
            
                    
                    Go 错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="../part22/">
            
                <a href="../part22/">
            
                    
                    Go 并发
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Go 语言函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="go-&#x8BED;&#x8A00;&#x51FD;&#x6570;">Go &#x8BED;&#x8A00;&#x51FD;&#x6570;</h1>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x51FD;&#x6570;&#x662F;&#x57FA;&#x672C;&#x7684;&#x4EE3;&#x7801;&#x5757;&#xFF0C;&#x7528;&#x4E8E;&#x6267;&#x884C;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x6700;&#x5C11;&#x6709;&#x4E2A; main() &#x51FD;&#x6570;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x51FD;&#x6570;&#x6765;&#x5212;&#x5206;&#x4E0D;&#x540C;&#x529F;&#x80FD;&#xFF0C;&#x903B;&#x8F91;&#x4E0A;&#x6BCF;&#x4E2A;&#x51FD;&#x6570;&#x6267;&#x884C;&#x7684;&#x662F;&#x6307;&#x5B9A;&#x7684;&#x4EFB;&#x52A1;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x544A;&#x8BC9;&#x4E86;&#x7F16;&#x8BD1;&#x5668;&#x51FD;&#x6570;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#xFF0C;&#x548C;&#x53C2;&#x6570;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x6807;&#x51C6;&#x5E93;&#x63D0;&#x4F9B;&#x4E86;&#x591A;&#x79CD;&#x53EF;&#x52A8;&#x7528;&#x7684;&#x5185;&#x7F6E;&#x7684;&#x51FD;&#x6570;&#x3002;&#x4F8B;&#x5982;&#xFF0C;len() &#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x5E76;&#x8FD4;&#x56DE;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x957F;&#x5EA6;&#x3002;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x4F20;&#x5165;&#x7684;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x5219;&#x8FD4;&#x56DE;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x957F;&#x5EA6;&#xFF0C;&#x5982;&#x679C;&#x4F20;&#x5165;&#x7684;&#x662F;&#x6570;&#x7EC4;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x6570;&#x7EC4;&#x4E2D;&#x5305;&#x542B;&#x7684;&#x5143;&#x7D20;&#x4E2A;&#x6570;&#x3002;</p>
<hr>
<h2 id="&#x51FD;&#x6570;&#x5B9A;&#x4E49;">&#x51FD;&#x6570;&#x5B9A;&#x4E49;</h2>
<p>Go &#x8BED;&#x8A00;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> function_name( [parameter list] ) [return_types] {
   &#x51FD;&#x6570;&#x4F53;
}
</code></pre>
<p>&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x89E3;&#x6790;&#xFF1A;</p>
<ul>
<li>func&#xFF1A;&#x51FD;&#x6570;&#x7531; func &#x5F00;&#x59CB;&#x58F0;&#x660E;</li>
<li>function_name&#xFF1A;&#x51FD;&#x6570;&#x540D;&#x79F0;&#xFF0C;&#x51FD;&#x6570;&#x540D;&#x548C;&#x53C2;&#x6570;&#x5217;&#x8868;&#x4E00;&#x8D77;&#x6784;&#x6210;&#x4E86;&#x51FD;&#x6570;&#x7B7E;&#x540D;&#xFF1B;</li>
<li>parameter list&#xFF1A;&#x53C2;&#x6570;&#x5217;&#x8868;&#xFF0C;&#x53C2;&#x6570;&#x5C31;&#x50CF;&#x4E00;&#x4E2A;&#x5360;&#x4F4D;&#x7B26;&#xFF0C;&#x5F53;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5C06;&#x503C;&#x4F20;&#x9012;&#x7ED9;&#x53C2;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x503C;&#x88AB;&#x79F0;&#x4E3A;&#x5B9E;&#x9645;&#x53C2;&#x6570;&#x3002;&#x53C2;&#x6570;&#x5217;&#x8868;&#x6307;&#x5B9A;&#x7684;&#x662F;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x3001;&#x987A;&#x5E8F;&#x3001;&#x53CA;&#x53C2;&#x6570;&#x4E2A;&#x6570;&#x3002;&#x53C2;&#x6570;&#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x51FD;&#x6570;&#x4E5F;&#x53EF;&#x4EE5;&#x4E0D;&#x5305;&#x542B;&#x53C2;&#x6570;&#x3002;</li>
<li>return_types&#xFF1A;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#xFF0C;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x5217;&#x503C;&#x3002;return_types &#x662F;&#x8BE5;&#x5217;&#x503C;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x3002;&#x6709;&#x4E9B;&#x529F;&#x80FD;&#x4E0D;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B; return_types &#x4E0D;&#x662F;&#x5FC5;&#x987B;&#x7684;&#x3002;</li>
<li>&#x51FD;&#x6570;&#x4F53;&#xFF1A;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x7684;&#x4EE3;&#x7801;&#x96C6;&#x5408;&#x3002;</li>
</ul>
<p>&#x5B9E;&#x4F8B;&#xFF1A;&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x4E3A; max() &#x51FD;&#x6570;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x8BE5;&#x51FD;&#x6570;&#x4F20;&#x5165;&#x4E24;&#x4E2A;&#x6574;&#x578B;&#x53C2;&#x6570; num1 &#x548C; num2&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x8FD9;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x6700;&#x5927;&#x503C;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    ret := max(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)
    fmt.Println(ret)
}

<span class="hljs-comment">/* &#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E24;&#x4E2A;&#x6570;&#x7684;&#x6700;&#x5927;&#x503C; */</span>
<span class="hljs-keyword">func</span> max(num1, num2 <span class="hljs-keyword">int</span>) <span class="hljs-keyword">int</span> {
    <span class="hljs-comment">/* &#x58F0;&#x660E;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
    <span class="hljs-keyword">var</span> result <span class="hljs-keyword">int</span>
    <span class="hljs-keyword">if</span> num1 &gt; num2 {
        result = num1
    } <span class="hljs-keyword">else</span> {
        result = num2
    }
    <span class="hljs-keyword">return</span> result
}
</code></pre>
<h2 id="&#x51FD;&#x6570;&#x8C03;&#x7528;">&#x51FD;&#x6570;&#x8C03;&#x7528;</h2>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x5F53;&#x521B;&#x5EFA;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x4F60;&#x5B9A;&#x4E49;&#x4E86;&#x51FD;&#x6570;&#x9700;&#x8981;&#x505A;&#x4EC0;&#x4E48;&#xFF0C;&#x901A;&#x8FC7;&#x8C03;&#x7528;&#x8BE5;&#x51FD;&#x6570;&#x6765;&#x6267;&#x884C;&#x6307;&#x5B9A;&#x4EFB;&#x52A1;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x8C03;&#x7528;&#x51FD;&#x6570;&#xFF0C;&#x5411;&#x51FD;&#x6570;&#x4F20;&#x9012;&#x53C2;&#x6570;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<p>&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
    <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">100</span>
    <span class="hljs-keyword">var</span> b <span class="hljs-keyword">int</span> = <span class="hljs-number">200</span>
    <span class="hljs-keyword">var</span> ret <span class="hljs-keyword">int</span>

    <span class="hljs-comment">/* &#x8C03;&#x7528;&#x51FD;&#x6570;&#x5E76;&#x8FD4;&#x56DE;&#x6700;&#x5927;&#x503C; */</span>
    ret = max(a, b)

    fmt.Printf( <span class="hljs-string">&quot;&#x6700;&#x5927;&#x503C;&#x662F; : %d\n&quot;</span>, ret )
}

<span class="hljs-comment">/* &#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E24;&#x4E2A;&#x6570;&#x7684;&#x6700;&#x5927;&#x503C; */</span>
<span class="hljs-keyword">func</span> max(num1, num2 <span class="hljs-keyword">int</span>) <span class="hljs-keyword">int</span> {
    <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
    <span class="hljs-keyword">var</span> result <span class="hljs-keyword">int</span>

    <span class="hljs-keyword">if</span> (num1 &gt; num2) {
        result = num1
    } <span class="hljs-keyword">else</span> {
        result = num2
    }
    <span class="hljs-keyword">return</span> result
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x5728; <code>main()</code> &#x51FD;&#x6570;&#x4E2D;&#x8C03;&#x7528; <code>max()</code>&#x51FD;&#x6570;&#xFF0C;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">&#x6700;&#x5927;&#x503C;&#x662F; : 200
</code></pre>
<hr>
<h2 id="&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x503C;">&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x503C;</h2>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x503C;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;
&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> swap(x, y <span class="hljs-keyword">string</span>) (<span class="hljs-keyword">string</span>, <span class="hljs-keyword">string</span>) {
   <span class="hljs-keyword">return</span> y, x
}

<span class="hljs-keyword">func</span> main() {
   a, b := swap(<span class="hljs-string">&quot;Google&quot;</span>, <span class="hljs-string">&quot;Apple&quot;</span>)
   fmt.Println(a, b)
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">Apple Google
</code></pre>
<hr>
<h2 id="&#x51FD;&#x6570;&#x53C2;&#x6570;">&#x51FD;&#x6570;&#x53C2;&#x6570;</h2>
<p>&#x51FD;&#x6570;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x53C2;&#x6570;&#xFF0C;&#x8BE5;&#x53D8;&#x91CF;&#x53EF;&#x79F0;&#x4E3A;&#x51FD;&#x6570;&#x7684;&#x5F62;&#x53C2;&#x3002;</p>
<p>&#x5F62;&#x53C2;&#x5C31;&#x50CF;&#x5B9A;&#x4E49;&#x5728;&#x51FD;&#x6570;&#x4F53;&#x5185;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x8C03;&#x7528;&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x6765;&#x4F20;&#x9012;&#x53C2;&#x6570;&#xFF1A;</p>
<table>
    <tr>
        <th bgcolor="#4b4b4b"><font color="#ffffff">&#x4F20;&#x9012;&#x7C7B;&#x578B;</font></th>
        <th bgcolor="#4b4b4b" width="100%"><font color="#ffffff">&#x63CF;&#x8FF0;</font></th>
    </tr>
    <tr>        
        <td align="center" bgcolor="#f5f2ee">&#x503C;&#x4F20;&#x9012;</td>
        <td align="center" bgcolor="#f5f2ee">&#x503C;&#x4F20;&#x9012;&#x662F;&#x6307;&#x5728;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x65F6;&#x5C06;&#x5B9E;&#x9645;&#x53C2;&#x6570;&#x590D;&#x5236;&#x4E00;&#x4EFD;&#x4F20;&#x9012;&#x5230;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x8FD9;&#x6837;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x5982;&#x679C;&#x5BF9;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#xFF0C;&#x5C06;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x5230;&#x5B9E;&#x9645;&#x53C2;&#x6570;</td>
    </tr>
    <tr>        
        <td align="center" bgcolor="#ffffff">&#x5F15;&#x7528;&#x4F20;&#x9012;</td>
        <td align="center" bgcolor="#ffffff">&#x5F15;&#x7528;&#x4F20;&#x9012;&#x662F;&#x6307;&#x5728;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x65F6;&#x5C06;&#x5B9E;&#x9645;&#x53C2;&#x6570;&#x7684;&#x5730;&#x5740;&#x4F20;&#x9012;&#x5230;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x90A3;&#x4E48;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x5BF9;&#x53C2;&#x6570;&#x6240;&#x8FDB;&#x884C;&#x7684;&#x4FEE;&#x6539;&#xFF0C;&#x5C06;&#x5F71;&#x54CD;&#x5230;&#x5B9E;&#x9645;&#x53C2;&#x6570;</td>
    </tr>
</table>

<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;Go &#x8BED;&#x8A00;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x503C;&#x4F20;&#x9012;&#xFF0C;&#x5373;&#x5728;&#x8C03;&#x7528;&#x8FC7;&#x7A0B;&#x4E2D;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x5230;&#x5B9E;&#x9645;&#x53C2;&#x6570;&#x3002;</p>
<h3 id="&#x503C;&#x4F20;&#x9012;">&#x503C;&#x4F20;&#x9012;</h3>
<p>&#x4F20;&#x9012;&#x662F;&#x6307;&#x5728;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x65F6;&#x5C06;&#x5B9E;&#x9645;&#x53C2;&#x6570;&#x590D;&#x5236;&#x4E00;&#x4EFD;&#x4F20;&#x9012;&#x5230;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x8FD9;&#x6837;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x5982;&#x679C;&#x5BF9;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#xFF0C;&#x5C06;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x5230;&#x5B9E;&#x9645;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;Go &#x8BED;&#x8A00;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x503C;&#x4F20;&#x9012;&#xFF0C;&#x5373;&#x5728;&#x8C03;&#x7528;&#x8FC7;&#x7A0B;&#x4E2D;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x5230;&#x5B9E;&#x9645;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x4EE5;&#x4E0B;&#x5B9A;&#x4E49;&#x4E86; swap() &#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x76F8;&#x4E92;&#x4EA4;&#x6362;&#x503C;&#x7684;&#x51FD;&#x6570; */</span>
<span class="hljs-keyword">func</span> swap(x, y <span class="hljs-keyword">int</span>) <span class="hljs-keyword">int</span> {
    <span class="hljs-keyword">var</span> temp <span class="hljs-keyword">int</span>

    temp = x <span class="hljs-comment">/* &#x4FDD;&#x5B58; x &#x7684;&#x503C; */</span>
    x = y    <span class="hljs-comment">/* &#x5C06; y &#x503C;&#x8D4B;&#x7ED9; x */</span>
    y = temp <span class="hljs-comment">/* &#x5C06; temp &#x503C;&#x8D4B;&#x7ED9; y*/</span>

    <span class="hljs-keyword">return</span> temp;
}
</code></pre>
<p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x503C;&#x4F20;&#x9012;&#x6765;&#x8C03;&#x7528; swap() &#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
    <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">100</span>
    <span class="hljs-keyword">var</span> b <span class="hljs-keyword">int</span> = <span class="hljs-number">200</span>

    fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x524D; a &#x7684;&#x503C;&#x4E3A; : %d\n&quot;</span>, a)
    fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x524D; b &#x7684;&#x503C;&#x4E3A; : %d\n&quot;</span>, b)

    <span class="hljs-comment">/* &#x901A;&#x8FC7;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x6765;&#x4EA4;&#x6362;&#x503C; */</span>
    swap(a, b)

    fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x540E; a &#x7684;&#x503C; : %d\n&quot;</span>, a)
    fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x540E; b &#x7684;&#x503C; : %d\n&quot;</span>, b)
}

<span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x76F8;&#x4E92;&#x4EA4;&#x6362;&#x503C;&#x7684;&#x51FD;&#x6570; */</span>
<span class="hljs-keyword">func</span> swap(x, y <span class="hljs-keyword">int</span>) <span class="hljs-keyword">int</span> {
    <span class="hljs-keyword">var</span> temp <span class="hljs-keyword">int</span>

    temp = x <span class="hljs-comment">/* &#x4FDD;&#x5B58; x &#x7684;&#x503C; */</span>
    x = y    <span class="hljs-comment">/* &#x5C06; y &#x503C;&#x8D4B;&#x7ED9; x */</span>
    y = temp <span class="hljs-comment">/* &#x5C06; temp &#x503C;&#x8D4B;&#x7ED9; y*/</span>

    <span class="hljs-keyword">return</span> temp
}
</code></pre>
<p>&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">&#x4EA4;&#x6362;&#x524D; a &#x7684;&#x503C;&#x4E3A; : 100
&#x4EA4;&#x6362;&#x524D; b &#x7684;&#x503C;&#x4E3A; : 200
&#x4EA4;&#x6362;&#x540E; a &#x7684;&#x503C; : 100
&#x4EA4;&#x6362;&#x540E; b &#x7684;&#x503C; : 200
</code></pre>
<p>&#x7A0B;&#x5E8F;&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x503C;&#x4F20;&#x9012;, &#x6240;&#x4EE5;&#x4E24;&#x4E2A;&#x503C;&#x5E76;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;&#x4EA4;&#x4E92;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>&#x5F15;&#x7528;&#x4F20;&#x9012;</code> &#x6765;&#x5B9E;&#x73B0;&#x4EA4;&#x6362;&#x6548;&#x679C;&#x3002;</p>
<p>&#x4EA4;&#x6362;&#x503C;&#x53EF;&#x4EE5;&#x8FD9;&#x4E48;&#x5199;&#xFF1A;</p>
<pre><code class="lang-go">a := <span class="hljs-number">100</span>
b := <span class="hljs-number">200</span>
a, b = b, a
<span class="hljs-comment">// a == 200</span>
<span class="hljs-comment">// b == 100</span>
</code></pre>
<h3 id="&#x5F15;&#x7528;&#x4F20;&#x9012;">&#x5F15;&#x7528;&#x4F20;&#x9012;</h3>
<p>&#x5F15;&#x7528;&#x4F20;&#x9012;&#x662F;&#x6307;&#x5728;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x65F6;&#x5C06;&#x5B9E;&#x9645;&#x53C2;&#x6570;&#x7684;&#x5730;&#x5740;&#x4F20;&#x9012;&#x5230;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x90A3;&#x4E48;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x5BF9;&#x53C2;&#x6570;&#x6240;&#x8FDB;&#x884C;&#x7684;&#x4FEE;&#x6539;&#xFF0C;&#x5C06;&#x5F71;&#x54CD;&#x5230;&#x5B9E;&#x9645;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x5F15;&#x7528;&#x4F20;&#x9012;&#x6307;&#x9488;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x5230;&#x51FD;&#x6570;&#x5185;&#xFF0C;&#x4EE5;&#x4E0B;&#x662F;&#x4EA4;&#x6362;&#x51FD;&#x6570; swap() &#x4F7F;&#x7528;&#x4E86;&#x5F15;&#x7528;&#x4F20;&#x9012;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x4EA4;&#x6362;&#x503C;&#x51FD;&#x6570;*/</span>
<span class="hljs-keyword">func</span> swap(x *<span class="hljs-keyword">int</span>, y *<span class="hljs-keyword">int</span>) {
    <span class="hljs-keyword">var</span> temp <span class="hljs-keyword">int</span>
    temp = *x    <span class="hljs-comment">/* &#x4FDD;&#x6301; x &#x5730;&#x5740;&#x4E0A;&#x7684;&#x503C; */</span>
    *x = *y      <span class="hljs-comment">/* &#x5C06; y &#x503C;&#x8D4B;&#x7ED9; x */</span>
    *y = temp    <span class="hljs-comment">/* &#x5C06; temp &#x503C;&#x8D4B;&#x7ED9; y */</span>
}
</code></pre>
<p>&#x4EE5;&#x4E0B;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x4F7F;&#x7528;&#x5F15;&#x7528;&#x4F20;&#x9012;&#x6765;&#x8C03;&#x7528; swap() &#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
    <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">100</span>
    <span class="hljs-keyword">var</span> b <span class="hljs-keyword">int</span>= <span class="hljs-number">200</span>

    fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x524D;&#xFF0C;a &#x7684;&#x503C; : %d\n&quot;</span>, a )
    fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x524D;&#xFF0C;b &#x7684;&#x503C; : %d\n&quot;</span>, b )

    <span class="hljs-comment">/* &#x8C03;&#x7528; swap() &#x51FD;&#x6570;
    * &amp;a &#x6307;&#x5411; a &#x6307;&#x9488;&#xFF0C;a &#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;
    * &amp;b &#x6307;&#x5411; b &#x6307;&#x9488;&#xFF0C;b &#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;
    */</span>
    swap(&amp;a, &amp;b)

    fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x540E;&#xFF0C;a &#x7684;&#x503C; : %d\n&quot;</span>, a )
    fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x540E;&#xFF0C;b &#x7684;&#x503C; : %d\n&quot;</span>, b )
}

<span class="hljs-keyword">func</span> swap(x *<span class="hljs-keyword">int</span>, y *<span class="hljs-keyword">int</span>) {
    <span class="hljs-keyword">var</span> temp <span class="hljs-keyword">int</span>
    temp = *x    <span class="hljs-comment">/* &#x4FDD;&#x5B58; x &#x5730;&#x5740;&#x4E0A;&#x7684;&#x503C; */</span>
    *x = *y      <span class="hljs-comment">/* &#x5C06; y &#x503C;&#x8D4B;&#x7ED9; x */</span>
    *y = temp    <span class="hljs-comment">/* &#x5C06; temp &#x503C;&#x8D4B;&#x7ED9; y */</span>
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">&#x4EA4;&#x6362;&#x524D;&#xFF0C;a &#x7684;&#x503C; : 100
&#x4EA4;&#x6362;&#x524D;&#xFF0C;b &#x7684;&#x503C; : 200
&#x4EA4;&#x6362;&#x540E;&#xFF0C;a &#x7684;&#x503C; : 200
&#x4EA4;&#x6362;&#x540E;&#xFF0C;b &#x7684;&#x503C; : 100
</code></pre>
<h2 id="&#x51FD;&#x6570;&#x7528;&#x6CD5;">&#x51FD;&#x6570;&#x7528;&#x6CD5;</h2>
<table>
    <tr>
        <th bgcolor="#4b4b4b"><font color="#ffffff">&#x51FD;&#x6570;&#x7528;&#x6CD5;</font></th>
        <th bgcolor="#4b4b4b" width="100%"><font color="#ffffff">&#x63CF;&#x8FF0;</font></th>
    </tr>
    <tr>        
        <td align="center" bgcolor="#f5f2ee">&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x53C2;</td>
        <td align="center" bgcolor="#f5f2ee">&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x540E;&#x53EF;&#x4F5C;&#x4E3A;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x53C2;&#x6570;&#x4F20;&#x5165;</td>
    </tr>
    <tr>        
        <td align="center" bgcolor="#ffffff">&#x95ED;&#x5305;</td>
        <td align="center" bgcolor="#ffffff">&#x95ED;&#x5305;&#x662F;&#x533F;&#x540D;&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x5728;&#x52A8;&#x6001;&#x7F16;&#x7A0B;&#x4E2D;&#x4F7F;&#x7528;</td>
    </tr>
    <tr>        
        <td align="center" bgcolor="#f5f2ee">&#x65B9;&#x6CD5;</td>
        <td align="center" bgcolor="#f5f2ee">&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x4E86;&#x63A5;&#x53D7;&#x8005;&#x7684;&#x51FD;&#x6570;</td>
    </tr>
</table>


<h3 id="&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x53C2;">&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x53C2;</h3>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x53EF;&#x4EE5;&#x5F88;&#x7075;&#x6D3B;&#x7684;&#x521B;&#x5EFA;&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x4F5C;&#x4E3A;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x53C2;&#x3002;&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x4E2D;&#x6211;&#x4EEC;&#x5728;&#x5B9A;&#x4E49;&#x7684;&#x51FD;&#x6570;&#x4E2D;&#x521D;&#x59CB;&#x5316;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x8BE5;&#x51FD;&#x6570;&#x4EC5;&#x4EC5;&#x662F;&#x4E3A;&#x4E86;&#x4F7F;&#x7528;&#x5185;&#x7F6E;&#x51FD;&#x6570; <code>math.sqrt()</code>&#x3002;
&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
   <span class="hljs-string">&quot;fmt&quot;</span>
   <span class="hljs-string">&quot;math&quot;</span>
)

<span class="hljs-keyword">func</span> main(){
    <span class="hljs-comment">/* &#x58F0;&#x660E;&#x51FD;&#x6570;&#x53D8;&#x91CF; */</span>
    getSquareRoot := <span class="hljs-keyword">func</span>(x <span class="hljs-keyword">float64</span>) <span class="hljs-keyword">float64</span> {
        <span class="hljs-keyword">return</span> math.Sqrt(x)
    }

    <span class="hljs-comment">/* &#x4F7F;&#x7528;&#x51FD;&#x6570; */</span>
    fmt.Println(getSquareRoot(<span class="hljs-number">9</span>))   <span class="hljs-comment">// 3</span>

}
</code></pre>
<p>&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#xFF0C;&#x5B9E;&#x73B0;&#x56DE;&#x8C03;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-comment">// &#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7C7B;&#x578B;</span>
<span class="hljs-keyword">type</span> cb <span class="hljs-keyword">func</span>(<span class="hljs-keyword">int</span>) <span class="hljs-keyword">int</span>

<span class="hljs-keyword">func</span> main() {
    testCallBack(<span class="hljs-number">1</span>, callBack)
    testCallBack(<span class="hljs-number">2</span>, <span class="hljs-keyword">func</span>(x <span class="hljs-keyword">int</span>) <span class="hljs-keyword">int</span> {
        fmt.Printf(<span class="hljs-string">&quot;&#x6211;&#x662F;&#x56DE;&#x8C03;&#xFF0C;x&#xFF1A;%d\n&quot;</span>, x)
        <span class="hljs-keyword">return</span> x
    })
}

<span class="hljs-keyword">func</span> testCallBack(x <span class="hljs-keyword">int</span>, f cb) {
    f(x)
}

<span class="hljs-keyword">func</span> callBack(x <span class="hljs-keyword">int</span>) <span class="hljs-keyword">int</span> {
    fmt.Printf(<span class="hljs-string">&quot;&#x6211;&#x662F;&#x56DE;&#x8C03;&#xFF0C;x&#xFF1A;%d\n&quot;</span>, x)
    <span class="hljs-keyword">return</span> x
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">&#x6211;&#x662F;&#x56DE;&#x8C03;&#xFF0C;x&#xFF1A;1
&#x6211;&#x662F;&#x56DE;&#x8C03;&#xFF0C;x&#xFF1A;2
</code></pre>
<h3 id="&#x95ED;&#x5305;">&#x95ED;&#x5305;</h3>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x652F;&#x6301;&#x533F;&#x540D;&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4F5C;&#x4E3A;&#x95ED;&#x5305;&#x3002;&#x533F;&#x540D;&#x51FD;&#x6570;&#x662F;&#x4E00;&#x4E2A;&quot;&#x5185;&#x8054;&quot;&#x8BED;&#x53E5;&#x6216;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x533F;&#x540D;&#x51FD;&#x6570;&#x7684;&#x4F18;&#x8D8A;&#x6027;&#x5728;&#x4E8E;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x5185;&#x7684;&#x53D8;&#x91CF;&#xFF0C;&#x4E0D;&#x5FC5;&#x7533;&#x660E;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E86;&#x51FD;&#x6570; <code>getSequence()</code> &#xFF0C;&#x8FD4;&#x56DE;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x3002;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x76EE;&#x7684;&#x662F;&#x5728;&#x95ED;&#x5305;&#x4E2D;&#x9012;&#x589E; <code>i</code> &#x53D8;&#x91CF;&#xFF0C;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> getSequence() <span class="hljs-keyword">func</span>() <span class="hljs-keyword">int</span> {
    i := <span class="hljs-number">0</span>
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">func</span>() <span class="hljs-keyword">int</span> {
        i += <span class="hljs-number">1</span>
        <span class="hljs-keyword">return</span> i
    }
}

<span class="hljs-keyword">func</span> main(){
    <span class="hljs-comment">/* nextNumber &#x4E3A;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x51FD;&#x6570; i &#x4E3A; 0 */</span>
    nextNumber := getSequence()

    <span class="hljs-comment">/* &#x8C03;&#x7528; nextNumber &#x51FD;&#x6570;&#xFF0C;i &#x53D8;&#x91CF;&#x81EA;&#x589E; 1 &#x5E76;&#x8FD4;&#x56DE; */</span>
    fmt.Println(nextNumber())
    fmt.Println(nextNumber())
    fmt.Println(nextNumber())

    <span class="hljs-comment">/* &#x521B;&#x5EFA;&#x65B0;&#x7684;&#x51FD;&#x6570; nextNumber1&#xFF0C;&#x5E76;&#x67E5;&#x770B;&#x7ED3;&#x679C; */</span>
    nextNumber1 := getSequence()
    fmt.Println(nextNumber1())
    fmt.Println(nextNumber1())
}
</code></pre>
<p>&#x4E00;&#x4E9B;&#x8865;&#x5145;</p>
<ol>
<li><p>&#x5E26;&#x53C2;&#x6570;&#x7684;&#x95ED;&#x5305;&#x51FD;&#x6570;&#x8C03;&#x7528;1&#xFF1A;</p>
<pre><code class="lang-go"> <span class="hljs-keyword">package</span> main

 <span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

 <span class="hljs-keyword">func</span> main() {
     addFunc := add(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>)
     fmt.Println(addFunc())  <span class="hljs-comment">// 1 3</span>
     fmt.Println(addFunc())  <span class="hljs-comment">// 2 3</span>
     fmt.Println(addFunc())  <span class="hljs-comment">// 3 3</span>
 }

 <span class="hljs-comment">// &#x95ED;&#x5305;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;</span>
 <span class="hljs-keyword">func</span> add(x1, x2 <span class="hljs-keyword">int</span>) <span class="hljs-keyword">func</span>()(<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>) {
     i := <span class="hljs-number">0</span>
     <span class="hljs-keyword">return</span> <span class="hljs-keyword">func</span>() (<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>){
         i++
         <span class="hljs-keyword">return</span> i, x1+x2
     }
 }
</code></pre>
</li>
<li>&#x5E26;&#x53C2;&#x6570;&#x7684;&#x95ED;&#x5305;&#x51FD;&#x6570;&#x8C03;&#x7528;2&#xFF1A;
```go
package main</li>
</ol>
<p>import &quot;fmt&quot;</p>
<p>func main() {
    addFunc := add(1,2)
    fmt.Println(addFunc(1,1))   // 1 3 2
    fmt.Println(addFunc(0,0))   // 2 3 0
    fmt.Println(addFunc(2,2))   // 3 3 4
}
// &#x95ED;&#x5305;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;
func add(x1, x2 int) func(x3 int, x4 int)(int, int, int)  {
    i := 0
    return func(x3 int, x4 int) (int, int, int){
        i++
        return i, x1+x2, x3+x4
    }
}</p>
<pre><code>
1. &#x5E26;&#x53C2;&#x6570;&#x7684;&#x95ED;&#x5305;&#x51FD;&#x6570;&#x8C03;&#x7528;3&#xFF1A;
```go
package main

import &quot;fmt&quot;

// &#x95ED;&#x5305;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#xFF0C;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x4E2D;&#x7684;&#x8FD4;&#x56DE;&#x503C;(&#x95ED;&#x5305;&#x51FD;&#x6570;)&#x4E0D;&#x7528;&#x5199;&#x5177;&#x4F53;&#x7684;&#x5F62;&#x53C2;&#x540D;&#x79F0;
func add(x1, x2 int) func(int, int) (int, int, int) {
    i := 0
    return func(x3, x4 int) (int, int, int) {
        i += 1
        return i, x1 + x2, x3 + x4
    }
}

func main() {
    addFunc := add(1, 2)
    fmt.Println(addFunc(4, 5))
    fmt.Println(addFunc(1, 3))
    fmt.Println(addFunc(2, 2))
}
</code></pre><h3 id="&#x65B9;&#x6CD5;">&#x65B9;&#x6CD5;</h3>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x4E2D;&#x540C;&#x65F6;&#x6709;&#x51FD;&#x6570;&#x548C;&#x65B9;&#x6CD5;&#x3002;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x4E86;&#x63A5;&#x53D7;&#x8005;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x63A5;&#x53D7;&#x8005;&#x53EF;&#x4EE5;&#x662F;&#x547D;&#x540D;&#x7C7B;&#x578B;&#x6216;&#x8005;&#x7ED3;&#x6784;&#x4F53;&#x7C7B;&#x578B;&#x7684;&#x4E00;&#x4E2A;&#x503C;&#x6216;&#x8005;&#x662F;&#x4E00;&#x4E2A;&#x6307;&#x9488;&#x3002;&#x6240;&#x6709;&#x7ED9;&#x5B9A;&#x7C7B;&#x578B;&#x7684;&#x65B9;&#x6CD5;&#x5C5E;&#x4E8E;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x65B9;&#x6CD5;&#x96C6;&#x3002;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> (variable_name variable_data_type) function_name() [return_type]{
   <span class="hljs-comment">/* &#x51FD;&#x6570;&#x4F53;*/</span>
}
</code></pre>
<p>&#x4E0B;&#x9762;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7ED3;&#x6784;&#x4F53;&#x7C7B;&#x578B;&#x548C;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x7ED3;&#x6784;&#x4F53; */</span>
<span class="hljs-keyword">type</span> Circle <span class="hljs-keyword">struct</span> {
    radius <span class="hljs-keyword">float64</span>
}

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> c1 Circle
    c1.radius = <span class="hljs-number">10.00</span>
    fmt.Println(<span class="hljs-string">&quot;&#x5706;&#x7684;&#x9762;&#x79EF; = &quot;</span>, c1.getArea())    <span class="hljs-comment">// &#x5706;&#x7684;&#x9762;&#x79EF; = 314</span>
}

<span class="hljs-comment">//&#x8BE5; method &#x5C5E;&#x4E8E; Circle &#x7C7B;&#x578B;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x65B9;&#x6CD5;</span>
<span class="hljs-keyword">func</span> (c Circle) getArea() <span class="hljs-keyword">float64</span> {
    <span class="hljs-comment">//c.radius &#x5373;&#x4E3A; Circle &#x7C7B;&#x578B;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x5C5E;&#x6027;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">3.14</span> * c.radius * c.radius
}
</code></pre>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x5173;&#x4E8E;&#x503C;&#x548C;&#x6307;&#x9488;&#xFF0C;&#x5982;&#x679C;&#x60F3;&#x5728;&#x65B9;&#x6CD5;&#x4E2D;&#x6539;&#x53D8;&#x7ED3;&#x6784;&#x4F53;&#x7C7B;&#x578B;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x9700;&#x8981;&#x5BF9;&#x65B9;&#x6CD5;&#x4F20;&#x9012;&#x6307;&#x9488;&#xFF0C;&#x4F53;&#x4F1A;&#x5982;&#x4E0B;&#x5BF9;&#x7ED3;&#x6784;&#x4F53;&#x7C7B;&#x578B;&#x6539;&#x53D8;&#x7684;&#x65B9;&#x6CD5; <code>changRadis()</code> &#x548C;&#x666E;&#x901A;&#x7684;&#x51FD;&#x6570; <code>change()</code> &#x4E2D;&#x7684;&#x6307;&#x9488;&#x64CD;&#x4F5C;:</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
    <span class="hljs-string">&quot;fmt&quot;</span>
)

<span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x7ED3;&#x6784;&#x4F53; */</span>
<span class="hljs-keyword">type</span> Circle <span class="hljs-keyword">struct</span> {
    radius <span class="hljs-keyword">float64</span>
}


<span class="hljs-keyword">func</span> main()  {
    <span class="hljs-keyword">var</span> c Circle
    fmt.Println(c.radius)    <span class="hljs-comment">// 0</span>
    c.radius = <span class="hljs-number">10.00</span>
    fmt.Println(c.getArea())    <span class="hljs-comment">// 314</span>
    c.changeRadius(<span class="hljs-number">20</span>)
    fmt.Println(c.radius)    <span class="hljs-comment">// 20</span>
    change(&amp;c, <span class="hljs-number">30</span>)
    fmt.Println(c.radius)    <span class="hljs-comment">// 30</span>
}
<span class="hljs-keyword">func</span> (c Circle) getArea() <span class="hljs-keyword">float64</span>  {
    <span class="hljs-keyword">return</span> <span class="hljs-number">3.14</span> * c.radius * c.radius
}
<span class="hljs-comment">// &#x6CE8;&#x610F;&#x5982;&#x679C;&#x60F3;&#x8981;&#x66F4;&#x6539;&#x6210;&#x529F;c&#x7684;&#x503C;&#xFF0C;&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x4F20;&#x6307;&#x9488;</span>
<span class="hljs-keyword">func</span> (c *Circle) changeRadius(radius <span class="hljs-keyword">float64</span>)  {
    c.radius = radius
}

<span class="hljs-comment">// &#x4EE5;&#x4E0B;&#x64CD;&#x4F5C;&#x5C06;&#x4E0D;&#x751F;&#x6548;</span>
<span class="hljs-comment">//func (c Circle) changeRadius(radius float64)  {</span>
<span class="hljs-comment">//  c.radius = radius</span>
<span class="hljs-comment">//}</span>
<span class="hljs-comment">// &#x5F15;&#x7528;&#x7C7B;&#x578B;&#x8981;&#x60F3;&#x6539;&#x53D8;&#x503C;&#x9700;&#x8981;&#x4F20;&#x6307;&#x9488;</span>
<span class="hljs-keyword">func</span> change(c *Circle, radius <span class="hljs-keyword">float64</span>)  {
    c.radius = radius
}
</code></pre>
<hr>
<h2 id="ps">PS</h2>
<ol>
<li><p>&#x6768;&#x8F89;&#x4E09;&#x89D2;&#x5F62;</p>
<pre><code class="lang-go"> <span class="hljs-keyword">package</span> main

 <span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

 <span class="hljs-comment">//&#x884C;&#x6570;</span>
 <span class="hljs-keyword">const</span> LINES <span class="hljs-keyword">int</span> = <span class="hljs-number">10</span>

 <span class="hljs-comment">// &#x6768;&#x8F89;&#x4E09;&#x89D2;</span>
 <span class="hljs-keyword">func</span> ShowYangHuiTriangle() {
     <span class="hljs-keyword">var</span> nums []<span class="hljs-keyword">int</span>
     <span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; LINES; i++ {
         <span class="hljs-comment">//&#x8865;&#x7A7A;&#x767D;</span>
         <span class="hljs-keyword">for</span> j := <span class="hljs-number">0</span>; j &lt; (LINES - i); j++ {
             fmt.Print(<span class="hljs-string">&quot; &quot;</span>)
         }
         <span class="hljs-keyword">for</span> j := <span class="hljs-number">0</span>; j &lt; (i + <span class="hljs-number">1</span>); j++ {
             <span class="hljs-keyword">var</span> length = <span class="hljs-built_in">len</span>(nums)
             <span class="hljs-keyword">var</span> value <span class="hljs-keyword">int</span>
             <span class="hljs-keyword">if</span> j == <span class="hljs-number">0</span> || j == i {
                 value = <span class="hljs-number">1</span>
             } <span class="hljs-keyword">else</span> {
                 value = nums[length - i] + nums[length - i - <span class="hljs-number">1</span>]
             }
             nums = <span class="hljs-built_in">append</span>(nums, value)
             fmt.Print(value, <span class="hljs-string">&quot; &quot;</span>)
         }
         fmt.Println(<span class="hljs-string">&quot;&quot;</span>)
     }
 }

 <span class="hljs-keyword">func</span> main() {
     ShowYangHuiTriangle()
 }
</code></pre>
<p> &#x7ED3;&#x679C;</p>
<pre><code class="lang-sh">          1 
         1 1 
        1 2 1 
       1 3 3 1 
      1 4 6 4 1 
     1 5 10 10 5 1 
    1 6 15 20 15 6 1 
   1 7 21 35 35 21 7 1 
  1 8 28 56 70 56 28 8 1 
 1 9 36 84 126 126 84 36 9 1
</code></pre>
</li>
<li><p>&#x4E5D;&#x4E5D;&#x4E58;&#x6CD5;&#x8868;</p>
<pre><code class="lang-go"> <span class="hljs-keyword">package</span> main

 <span class="hljs-keyword">import</span> (
     <span class="hljs-string">&quot;fmt&quot;</span>
     <span class="hljs-string">&quot;strconv&quot;</span>
 )

 <span class="hljs-keyword">func</span> multiplicationTable() {
     <span class="hljs-keyword">for</span> i := <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">10</span>; i++ {
         <span class="hljs-keyword">for</span> j := <span class="hljs-number">1</span>; j &lt;= i; j++ {
             <span class="hljs-keyword">var</span> ret <span class="hljs-keyword">string</span>
             <span class="hljs-keyword">if</span> i * j &lt; <span class="hljs-number">10</span> &amp;&amp; j != <span class="hljs-number">1</span> {
                 ret = <span class="hljs-string">&quot; &quot;</span> + strconv.Itoa(i * j)
             } <span class="hljs-keyword">else</span> {
                 ret = strconv.Itoa(i * j)
             }

             fmt.Print(j, <span class="hljs-string">&quot; x &quot;</span>, i, <span class="hljs-string">&quot; = &quot;</span>, ret, <span class="hljs-string">&quot;   &quot;</span>)
         }
         fmt.Println(<span class="hljs-string">&quot;&quot;</span>)
     }
 }

 <span class="hljs-keyword">func</span> main() {
     multiplicationTable()
 }
</code></pre>
<p> &#x7ED3;&#x679C;</p>
<pre><code class="lang-sh"> 1 x 1 = 1   
 1 x 2 = 2   2 x 2 =  4   
 1 x 3 = 3   2 x 3 =  6   3 x 3 =  9   
 1 x 4 = 4   2 x 4 =  8   3 x 4 = 12   4 x 4 = 16   
 1 x 5 = 5   2 x 5 = 10   3 x 5 = 15   4 x 5 = 20   5 x 5 = 25   
 1 x 6 = 6   2 x 6 = 12   3 x 6 = 18   4 x 6 = 24   5 x 6 = 30   6 x 6 = 36   
 1 x 7 = 7   2 x 7 = 14   3 x 7 = 21   4 x 7 = 28   5 x 7 = 35   6 x 7 = 42   7 x 7 = 49   
 1 x 8 = 8   2 x 8 = 16   3 x 8 = 24   4 x 8 = 32   5 x 8 = 40   6 x 8 = 48   7 x 8 = 56   8 x 8 = 64   
 1 x 9 = 9   2 x 9 = 18   3 x 9 = 27   4 x 9 = 36   5 x 9 = 45   6 x 9 = 54   7 x 9 = 63   8 x 9 = 72   9 x 9 = 81
</code></pre>
</li>
<li><p>&#x83F1;&#x5F62;</p>
<pre><code class="lang-go"> <span class="hljs-keyword">package</span> main

 <span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

 <span class="hljs-keyword">func</span> main() {
     <span class="hljs-comment">// &#x957F;</span>
     x := <span class="hljs-number">9</span>
     <span class="hljs-comment">// &#x5BBD;</span>
     y := <span class="hljs-number">9</span>
     <span class="hljs-comment">// &#x884C;&#x6570;</span>
     row := <span class="hljs-number">1</span>
     <span class="hljs-keyword">for</span> row &lt;= y {
         <span class="hljs-comment">// &#x8BA1;&#x7B97;&#x6BCF;&#x884C;&#x5F97;&#x661F;&#x661F;&#x6570;</span>
         count := <span class="hljs-number">0</span>
         <span class="hljs-keyword">if</span> row &lt;= (y / <span class="hljs-number">2</span> + <span class="hljs-number">1</span>) {
             count = <span class="hljs-number">2</span> * row - <span class="hljs-number">1</span>
         } <span class="hljs-keyword">else</span> {
             count = <span class="hljs-number">2</span> * (y - row) + <span class="hljs-number">1</span>
         }
         row++
         text := <span class="hljs-string">&quot;&quot;</span>
         <span class="hljs-comment">// &#x7B97;&#x51FA;&#x663E;&#x793A;&#x661F;&#x661F;&#x7684;&#x8303;&#x56F4;</span>
         starMin := ((x - count) / <span class="hljs-number">2</span>) + <span class="hljs-number">1</span>
         starMax := ((x - count) / <span class="hljs-number">2</span>) + count
         <span class="hljs-keyword">for</span> index := <span class="hljs-number">1</span>; index &lt;= x; index++ {
             <span class="hljs-keyword">if</span> index &gt;= starMin &amp;&amp; index &lt;= starMax {
                 text += <span class="hljs-string">&quot;*&quot;</span>
             } <span class="hljs-keyword">else</span> {
                 text += <span class="hljs-string">&quot; &quot;</span>
             }
         }
         fmt.Println(text)
     }
 }
</code></pre>
<p> &#x7ED3;&#x679C;</p>
<pre><code class="lang-sh">     *    
    ***   
   *****  
  ******* 
 *********
  ******* 
   *****  
    ***   
     *
</code></pre>
</li>
</ol>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../part10/" class="navigation navigation-prev " aria-label="Previous page: Go 语言循环语句">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../part12/" class="navigation navigation-next " aria-label="Next page: Go 语言数组">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Go 语言函数","level":"1.12","depth":1,"next":{"title":"Go 语言数组","level":"1.13","depth":1,"path":"part12/README.md","ref":"part12/README.md","articles":[]},"previous":{"title":"Go 语言循环语句","level":"1.11","depth":1,"path":"part10/README.md","ref":"part10/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"part11/README.md","mtime":"2021-01-19T11:30:22.584Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-01-21T06:29:04.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

